package com.cuit.zuke.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.zuke.common.vo.QuarterlyDataVo;
import com.cuit.zuke.hotel.model.hotel.Hotel;
import com.cuit.zuke.hotel.model.hotel.HotelRoom;
import com.cuit.zuke.order.client.HotelFeignClient;
import com.cuit.zuke.order.mapper.GoodsOrderMapper;
import com.cuit.zuke.order.mapper.HotelOrderMapper;
import com.cuit.zuke.order.model.entity.GoodsOrder;
import com.cuit.zuke.order.model.entity.HotelOrder;
import com.cuit.zuke.order.model.vo.GoodsOrderVO;
import com.cuit.zuke.order.model.vo.HotelOrderQueryVO;
import com.cuit.zuke.order.model.vo.HotelOrderVO;
import com.cuit.zuke.order.service.GoodsOrderService;
import com.cuit.zuke.order.service.HotelOrderService;
import com.cuit.zuke.product.model.entity.Goods;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class HotelOrderServiceImpl  extends ServiceImpl<HotelOrderMapper, HotelOrder> implements HotelOrderService {

    @Autowired
    private HotelFeignClient hotelFeignClient;

    @Override
    public Page<HotelOrderVO> findByPage(Integer current, Integer limit, HotelOrderQueryVO hotelOrderQueryVO) {
        Page<HotelOrder> hotelOrderPage = new Page<>(current, limit);
        Page<HotelOrderVO> hotelOrderVOPage = new Page<>(hotelOrderPage.getCurrent(), hotelOrderPage.getSize());
        String orderId = hotelOrderQueryVO.getOrderId();
        String userId = hotelOrderQueryVO.getUserId();
        String status = hotelOrderQueryVO.getStatus();
        //构建查询条件
        QueryWrapper<HotelOrder> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(orderId)){
            wrapper.eq("order_id",orderId);
        }
        if(!StringUtils.isEmpty(userId)){
            wrapper.eq("user_id",userId);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }

        hotelOrderPage = baseMapper.selectPage(hotelOrderPage, wrapper);
        List<HotelOrder> records = hotelOrderPage.getRecords();
        List<HotelOrderVO> hotelOrderVOS = new ArrayList<>();
        // HotelOrder -> HotelOrderVO
        for (HotelOrder hotelOrder : records) {
            HotelOrderVO hotelOrderVO = new HotelOrderVO();
            BeanUtils.copyProperties(hotelOrder,hotelOrderVO);
            Map<String, Object> hotelAndRoom = hotelFeignClient.getHotelAndRoom(hotelOrder.getZukeCode(), hotelOrder.getRoomCode());
            ObjectMapper mapper = new ObjectMapper();
            Hotel hotel = mapper.convertValue(hotelAndRoom.get("hotel"), Hotel.class);
            HotelRoom room = mapper.convertValue(hotelAndRoom.get("room"), HotelRoom.class);
            hotelOrderVO.setHotel(hotel);
            hotelOrderVO.setRoom(room);
            Integer to = hotelOrderVO.getBookDays() * hotelOrderVO.getRoom().getRoomprice();
            hotelOrderVO.setTotal(to.doubleValue());
            hotelOrderVOS.add(hotelOrderVO);
        }
        hotelOrderVOPage.setRecords(hotelOrderVOS);
        hotelOrderVOPage.setTotal(hotelOrderPage.getTotal());
        return hotelOrderVOPage;
    }

    @Override
    public boolean deleteById(Long id) {
        int i = baseMapper.deleteById(id);
        return i>0;
    }

}
